﻿namespace Baggr.FeedParser.Atom
{
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Xml.Serialization;

    [Serializable]
    [DebuggerStepThrough]
    [DesignerCategory("code")]
    [XmlType(Namespace = "http://www.w3.org/2005/Atom")]
    [XmlRoot("entry", Namespace = "http://www.w3.org/2005/Atom", IsNullable = false)]
    public class AtomEntity
        : IFeedItem
    {
        /// <summary>
        /// Описание записи.
        /// </summary>
        string IFeedItem.Description
        {
            get
            {
                if (Summary == null)
                    return string.Empty;

                if (Summary.Text == null)
                    return string.Empty;

                return Summary.Text;
            }
        }

        /// <summary>
        /// Автор записи.
        /// </summary>
        string IFeedItem.Author
        {
            get
            {
                if (Author == null)
                    return string.Empty;

                if (!string.IsNullOrEmpty(Author.Email))
                    return Author.Email;

                if (!string.IsNullOrEmpty(Author.Name))
                    return Author.Name;

                if (!string.IsNullOrEmpty(Author.Uri))
                    return Author.Uri;

                return string.Empty;
            }
        }

        /// <summary>
        /// Дата публикации.
        /// </summary>
        DateTime IFeedItem.PublicationDate
        {
            get
            {
                if (!string.IsNullOrEmpty(Published))
                    return new DateTime();

                return DateTime.Parse(Published);
            }
        }

        /// <summary>
        /// Локальный идентификатор записи.
        /// </summary>
        Guid IFeedItem.LocalId
        {
            get
            {
                if (!string.IsNullOrEmpty(Id))
                    return Id.GetGuidByString();

                var thisFeed = (IFeedItem) this;
                return (thisFeed.Title + thisFeed.Description).GetGuidByString();
            }
        }

        [XmlElement("author")]
        public AtomPerson Author { get; set; }

        [XmlElement("category")]
        public AtomCategory Category { get; set; }

        [XmlElement("content")]
        public AtomContent Content { get; set; }

        [XmlElement("contributor")]
        public AtomPerson Contributor { get; set; }

        [XmlElement("id")]
        public string Id { get; set; }

        /// <summary>
        /// Заглавие записи.
        /// </summary>
        string IFeedItem.Title
        {
            get
            {
                if (Title == null)
                    return null;

                return Title.Text;
            }
        }

        /// <summary>
        /// Ссылка на запись.
        /// </summary>
        Uri IFeedItem.Link
        {
            get
            {
                if (Link == null)
                    return null;
                if (string.IsNullOrEmpty(Link.Href))
                    return null;
                return new Uri(Link.Href);
            }
        }

        [XmlElement("link")]
        public AtomLink Link { get; set; }

        [XmlElement("published")]
        public string Published { get; set; }

        [XmlElement("rights")]
        public AtomText Rights { get; set; }

        [XmlElement("source")]
        public AtomText Source { get; set; }

        [XmlElement("summary")]
        public AtomText Summary { get; set; }

        [XmlElement("title")]
        public AtomText Title { get; set; }

        [XmlIgnore]
        public DateTime Updated { get; set; }

        [XmlElement("updated")]
        public string UpdatedAsString
        {
            get
            {
                return Updated.ToString();
            }
            set
            {
                Updated = DateTime.Parse(value);
            }
        }
    }
}
